<template>
    <div>
        <input type="text" v-model="title">
        <input type="text" v-model="con">
        <button @click="add">发表博客</button>
    </div>
</template>

<script>
import request from '../util/request'
export default {
    data(){
        return {
            title:'',
            con:'',
            username:'tom',
            id:null
        }
    },
    created(){
        let item = JSON.parse(localStorage.getItem('item'));
        this.title = item.title;
        this.con = item.con;
        this.id = item.id;
    },
    methods:{
        //添加
        add(){
            let url = '',  //请求地址
                type = ''; //请求方式
            if(this.id){
                //编辑
                url = '/api/update';
                type = 'put';
            }else{
                url = '/api/add';
                type = 'post';
            }
            request[type](url,{title:this.title,con:this.con,username:this.username,id:this.id}).then(res => {
                console.log("成功");
                if(res.data.code === 1){
                    localStorage.clear();
                    this.$router.push('/home')
                }
            })
        }
    }
}
</script>
